<?php
namespace App\Console\Commands;

use Illuminate\Console\Command;

use App\Model\BotsModel;
use App\Model\OutTradeModel;
use waylaidwanderer\SteamCommunity\MobileAuth\Confirmations\Confirmation;
use App\Service\BotsService;
use App\Service\SteamService;


class AcceptTrade extends Command
{
	/**
	 * The console command name.
	 *
	 * @var string
	 */
	protected $signature = 'accept:trade';

	/**
	 * The console command description.
	 *
	 * @var string
	 */
	protected $description = '确认报价.';

	/**
	 * Create a new command instance.
	 *
	 * @return void
	 */
	public function __construct()
	{
		parent::__construct();
	}

	/**
	 * 获取等待确认的报价
	 * @return array
	 */
	protected function getOutTradeByWaitAccept()
	{
		return OutTradeModel::getOutTradeByWaitAccept();
	}

	/**
	 * 获取机器人信息
	 * @param  int $botId 机器人ID
	 * @return array
	 */
	protected function getBotsInfoBybotId($botId)
	{
		return BotsModel::getBotsInfoBybotId($botId);
	}

	/**
	 * 修改报价状态
	 * @param  int $outTradeId 报价订单ID
	 * @param  int $tradeofferStatus 报价状态
	 * @return bool
	 */
	protected function updateOutTradeState($outTradeId, $tradeofferStatus)
	{
		return OutTradeModel::updateOutTradeState($outTradeId, $tradeofferStatus);
	}

	/**
	 * 多报价处理
	 * @return bool
	 */
	protected function acceptTradeAll()
	{
		$trades = $this->getOutTradeByWaitAccept();
		if (!$trades) {
			echo 'query waiting for accept failed'."\r\n";
			return false;
		}

		foreach ($trades as $trade) {
			$this->acceptTrade($trade['bot_id'], $trade);
		}
		
		return true;
	}

	/**
	 * 单报价处理
	 * @return bool
	 */
	protected function acceptTrade($botId, $trade)
	{
		$info = $this->getBotsInfoBybotId($botId);
		if (!$info) {
			echo 'query bots failed, botid: '.$botId."\r\n";
			return false;
		}

		$apiKey = $info['apikey'];
		$account = $info['steam_account'];

		$steam = BotsService::getInstance()->getLgoinObjByBotId($botId, $account);
		if (!$steam) {
            echo 'query bot object failed, bot_id: '.$botId."\r\n";
			return false;
		}

		//query trade offer info
		$tradeoffer = SteamService::getInstance()->getTradeoffer($apiKey, $trade['tradeoffer_id']);
		if (empty($tradeoffer)) {
			echo 'query trade offer failed, tradeoffer_id:'.$trade['tradeoffer_id']."\r\n";
			return false;
		}

		$tradeofferid = $tradeoffer['tradeofferid'];
		$tradestate = $tradeoffer['trade_offer_state'];

		if ($tradestate != OutTradeModel::$tradeStatusMap['creating']) {
			$res = $this->updateOutTradeState($tradeofferid, $tradestate);
			if (!$res) {
				echo 'update out trade status failed, tradeoffer_id:'.$trade['tradeoffer_id']."\r\n";
				return false;
			}
			echo 'waiting for accept, trade offer state changes';
			return false;
		}

		$confirmation = new Confirmation($trade['conf_id'], $trade['conf_key'], $trade['tradeoffer_id'], '');

		$time = 1;
		$status = false;
		while ($time < 3) {
			$status = $steam->mobileAuth()->confirmations()->acceptConfirmation($confirmation);
			sleep(3);
			$time += 1;
			if ($status) {
				break;
			}
		}

		if ($status === true) {
			$res = $this->updateOutTradeState($trade['out_trade_id'], OutTradeModel::$tradeStatusMap['waiting']);
			if (!$res) {
				echo 'update out trade status failed, tradeoffer_id:'.$trade['tradeoffer_id']."\r\n";
				return false;
			}
		}

		echo 'update out trade status success'."\r\n";
		return true;
	}

	/**
	 * Execute the console command.
	 *
	 * @return mixed
	 */
	public function handle()
	{
		$this->acceptTradeAll();
		echo '数据查询完毕!'."\r\n";
		return true;
	}
}